package com.yyt.module_main.act;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yyt.libsrc.bean.AccountOverviewBean;
import com.yyt.libsrc.bean.BasicDocumentBean;
import com.yyt.libsrc.bean.ConfigBean;
import com.yyt.libsrc.bean.GetRatesBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityWithdrawBinding;
import com.yyt.module_main.vm.WithdrawVM;

import top.lixb.libcommon.base.BaseActivity;
import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_withdraw)
public class WithdrawActivity extends RefreshableActivity<ActivityWithdrawBinding> {

    private WithdrawVM mVm;

    @Override
    protected void init() {
        mVm = new WithdrawVM();
        mRefreshContentBinding.setVm(mVm);
        setTitle("立即提现");
        showToolbarDivider();
        SpannableString str = new SpannableString("请输入提现金额至少满足100元");
        AbsoluteSizeSpan textsize = new AbsoluteSizeSpan(14, true);
        str.setSpan(textsize, 0,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mRefreshContentBinding.etAmount.setHint(str);
        
    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_withdraw;
    }


    @Override
    protected void getData(boolean refresh) {
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.basicDocument)
                .build(), new SuccessCallback<BasicDocumentBean>() {
            @Override
            public void onSuccess(BasicDocumentBean basicDocumentBean) {
                mVm.bankname.set(basicDocumentBean.data.bankName);
                mVm.cardId.set(basicDocumentBean.data.bankNo);
                mVm.cardOwner.set("持卡人:"+basicDocumentBean.data.realName);
                getBalance();


                getRateSetting();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                finishGetData();
            }
        });
    }

    private void getRateSetting() {
//        NetUtil.sellerPost(new JavaParamsBuilder()
//                .addCmd(Consts.CMD.getRates)
//                .addP("id","10008")
//                .build(), new SuccessCallback<GetRatesBean>() {
//            @Override
//            public void onSuccess(GetRatesBean getRatesBean) {
//                String[] split = getRatesBean.data.config_value.split("-");
//                if (null!=split && split.length==2) {
//                    String feeRate = split[0];
//                    String channelFee = split[1];
//                    float ffr = Float.valueOf(feeRate) * 100;
//                    mVm.fee.set("手续费"+ ffr +"%+"+channelFee+"(预计2小时之内到账)");
//                }
//            }
//        });


        //提现收取费率
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.configuration)
                .addP("name", "reflect_costs")
                .build(), new SuccessCallback<ConfigBean>() {
            @Override
            public void onSuccess(ConfigBean configBean) {
                if (!TextUtils.isEmpty(configBean.data.value)) {
                    mVm.reflect_costs.set(String.valueOf(Float.valueOf(configBean.data.value) * 100));
                }
                NetUtil.sellerPost(new JavaParamsBuilder()
                        .addCmd(Consts.CMD.configuration)
                        .addP("name","channelFee")
                        .build(), new SuccessCallback<ConfigBean>() {
                    @Override
                    public void onSuccess(ConfigBean configBean) {
                        mVm.channelFee.set(configBean.data.value);
                        int feerate = Double.valueOf(mVm.reflect_costs.get()).intValue();
                        mVm.fee.set("手续费"+ feerate +"%(预计2小时之内到账)");
                    }
                });
            }
        });
    }

    private void getBalance() {
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.accountOverview)
                .build(), new SuccessCallback<AccountOverviewBean>() {
            @Override
            public void onSuccess(AccountOverviewBean accountOverviewBean) {
                double v = Double.valueOf(accountOverviewBean.data.recommendedBalance) + Double.valueOf(accountOverviewBean.data.bonusBalance);
                mVm.balance.set(StringUtils.doubleFormat(String.valueOf(v)));
            }

            @Override
            public void onComplete() {
                super.onComplete();
                finishGetData();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData(true);
        mVm.withdrawAmount.set("");
    }
}
